package com.dd.front.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.dd.common.pojo.dto.FileUploadDTO;
import com.dd.common.pojo.vo.UploadFilesVo;
import com.dd.common.service.SysFileService;
import com.dd.common.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

@SaIgnore
@Slf4j
@Tag(name = "文件管理", description = "文件管理接口")
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private SysFileService sysFileService;

    /**
     * 上传文件
     */
    @Operation(summary = "上传文件") //consumes 指定该方法只处理特定Content-Type的请求
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult<UploadFilesVo> uploadFile(@Validated FileUploadDTO dto) {
        return AjaxResult.success(sysFileService.uploadFile(dto.getFile(), dto));
    }

    @Operation(summary = "文件上传检查", description = "hash检查文件是否已经存在，true表示已存在")
    @PostMapping("/check")
    public AjaxResult<Boolean> checkFile(@NotBlank(message = "hash不能为空") String hash) {
        return AjaxResult.success(sysFileService.checkFile(hash) != null);
    }
}
